package com.jams.goodox;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.jams.goodox.update.UpDateVersionUtils;
import com.jams.goodox.utils.TimeUtils;

import static android.content.Context.MODE_PRIVATE;

/**
 *
 * Created by DWCloud on 2016/4/19.
 */
public class SePref {

    public static final String TABLE_NAME = "SE_PREF";

    public static final  String TABLE_SYSTEM= TownApplicationLike.getInstance().getPackageName();

    public static final String USER_INFO = "user_info";
    public static final String USER_NAME = "username";
    public static final String USER_TOKEN = "token";
    public static final String USER_ALISA = "alisa";
    public static final String UpDateTime = "UpDateTime";
    public static final String HomeRefreshTime = "HomeRefreshTime";
    public static final String SocialRefreshTime = "SocialRefreshTime";

    public static final String PATCH_INFO="patch";//此和用户无关，用于存储当前版本的热修复补丁相应信息，禁止清除



    public static boolean put(String key, Object obj) {
        String type = obj.getClass().getSimpleName();
        SharedPreferences sp = TownApplicationLike.getInstance().getSharedPreferences(TABLE_NAME,
                MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        switch (type) {
            case "String":
                editor.putString(key, (String) obj);
                break;
            case "Integer":
                editor.putInt(key, (Integer) obj);
                break;
            case "Boolean":
                editor.putBoolean(key, (Boolean) obj);
                break;
            case "Float":
                editor.putFloat(key, (Float) obj);
                break;
            case "Long":
                editor.putLong(key, (Long) obj);
                break;
        }

        return editor.commit();
    }


    public static Object get(String key, Object defObj) {
        String type = defObj.getClass().getSimpleName();
        SharedPreferences sp = TownApplicationLike.getInstance().getSharedPreferences(TABLE_NAME,
                MODE_PRIVATE);
        switch (type) {
            case "String":
                return sp.getString(key, (String) defObj);
            case "Integer":
                return sp.getInt(key, (Integer) defObj);
            case "Boolean":
                return sp.getBoolean(key, (Boolean) defObj);
            case "Float":
                return sp.getFloat(key, (Float) defObj);
            case "Long":
                return sp.getLong(key, (Long) defObj);
        }

        return null;
    }

    public static boolean clear() {
        SharedPreferences sp = TownApplicationLike.getInstance().getSharedPreferences(TABLE_NAME,
                MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        return editor.commit();
    }


    public static void clearUserInfo() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, TownApplicationLike.getInstance().MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }

    public static void clearSystemInfo() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, TownApplicationLike.getInstance().MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }


    public static String getCommunityId() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString("communityId", "");
    }

    public static boolean putCommunityId( String data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("communityId", data);
        return editor.commit();
    }

    public static String getCommunityName() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString("CommunityName", "");
    }

    public static boolean putCommunityName(String data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("CommunityName", data);
        return editor.commit();
    }

    public static long getCoinCount() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return Long.parseLong(sharedPreferences.getString("CommunityName", ""));
    }




    public static String getPrePhone() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getString("prephone", "");
    }

    public static boolean setPrePhone( String data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("prephone", data);
        return editor.commit();
    }


    //用户类型（1家长，2教师）
    public static int getMemberType() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getInt("memberType", 1);
    }

    public static void setMemberType( int data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("memberType", data);
        editor.apply();
    }

    public static String getHeadImageUrl() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getString("HeadImageUrl", "");
    }

    public static void setHeadImageUrl( String data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("HeadImageUrl", data);
        editor.apply();
    }


    public static String getCity() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getString("City", "--");
    }

    public static void setCity( String data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("City", data);
        editor.apply();
    }


    public static String getNickName() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getString(USER_NAME, "--");
    }

    public static void setNickName( String data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(USER_NAME, data);
        editor.apply();
    }


    public static int getId() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getInt("id", 0);
    }

    public static void setId( int data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("id", data);
        editor.apply();
    }

    public static boolean setFunctionOpened(String key, boolean opened){
        if(TextUtils.isEmpty(key))return false;
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, opened);
        return editor.commit();
    }

    public static boolean getFunctionIsOpen(String key){
        if(TextUtils.isEmpty(key))return false;
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, false);
    }

    public static String getGuide() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getString("Guide", "");
    }

    public static boolean setGuide( String data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("Guide", data);
        return editor.commit();
    }


    public static int getForce() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getInt("update", 0);
    }

    public static boolean putForce(int update) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("update", update);
        return editor.commit();
    }

    public static String getToday() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getString("today", "");
    }

    public static boolean putToday(String today) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("today", today);
        return editor.commit();
    }


    public static String getUnionFirstInstall() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        return sharedPreferences.getString("UnionFirstInstall", "");
    }

    public static boolean setUnionFirstInstall(String data) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(TABLE_SYSTEM, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("UnionFirstInstall", data);
        return editor.commit();
    }

    public static boolean putUserPhone( String obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("UserPhone", obj);
        return editor.commit();
    }

    public static String getUserPhone() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString("UserPhone", "");
    }


    public static boolean putIndividualitySign( String obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("individualitySign", obj);
        return editor.commit();
    }

    public static String getIndividualitySign() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString("individualitySign", "");
    }



    public static void putGrade( String obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("grade", obj);
        editor.apply();
    }

    public static String getGrade() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString("grade", "");
    }

    public static boolean setHasPayPwd(boolean obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("PayPwd", obj);
        return editor.commit();
    }

    public static boolean isSetPayPwd() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getBoolean("PayPwd", false);
    }

    /**
     * ------------------------------------------
     *
     * @return
     * @desc 获取用户名
     * ------------------------------------------
     */
    public static String getUserId() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString("UserId", "");
    }

    public static boolean putUserId( String obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("UserId", obj);
        return editor.commit();
    }

    public static String getCompanyId() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString("CompanyId", "");
    }


    public static boolean putCompanyId( String obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("CompanyId", obj);
        return editor.commit();
    }


    /**
     * ------------------------------------------
     *
     * @param obj
     * @return
     * @desc 存放token
     * ------------------------------------------
     */
    public static boolean putToken( String obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(USER_TOKEN, obj);
        return editor.commit();
    }


    public static String getToken() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString(USER_TOKEN, "");
    }

    public static boolean isLogin() {
        return !TextUtils.isEmpty(getToken());
    }

    /**
     * ------------------------------------------
     *
     * @param obj
     * @return
     * @desc 存放推送密钥
     * ------------------------------------------
     */
    public static boolean putAlisa( String obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(USER_ALISA, obj);
        return editor.commit();
    }


    public static String getAlisa() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getString(USER_ALISA, "");

    }

    // 用户有多少积分
    public static boolean putUserPoints( int obj) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("UserPoints", obj);
        return editor.commit();
    }

    public static int getUserPoints() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(USER_INFO, MODE_PRIVATE);
        return sharedPreferences.getInt("UserPoints", 0);
    }

    public static boolean putUpDateTime() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(UpDateTime, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(UpDateTime, UpDateVersionUtils.getSystemTime());
        return editor.commit();
    }

    //获取下载提示时间
    public static String getUpDateTime() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(UpDateTime, MODE_PRIVATE);
        return sharedPreferences.getString(UpDateTime, "0000/00/00 00:00:00");
    }

    public static boolean putSocialRefreshTime() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(SocialRefreshTime, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SocialRefreshTime, TimeUtils.getSystemTime());
        return editor.commit();
    }

    //获取圈子提示时间
    public static String getSocialRefreshTime() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(SocialRefreshTime, MODE_PRIVATE);
        return sharedPreferences.getString(SocialRefreshTime, "0000-00-00 00:00:00");
    }

    public static boolean putHomeRefreshTime() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(HomeRefreshTime, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(HomeRefreshTime, TimeUtils.getSystemTime());
        return editor.commit();
    }

    //获取圈子提示时间
    public static String getHomeRefreshTime() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(HomeRefreshTime, MODE_PRIVATE);
        return sharedPreferences.getString(HomeRefreshTime, "0000-00-00 00:00:00");
    }

    /**              补丁相关             **/

    /**
     * 存储当前获取到的补丁版本号
     * @param patchCode 补丁版本号
     * @return
     */
    public static boolean putCurrentPatchCode(String patchCode) {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(PATCH_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("patchCode", patchCode);
        return editor.commit();
    }

    /**
     * 获取当前已知的补丁版本号
     * @return 补丁版本号
     */
    public static String getCurrentPatchCode() {
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(PATCH_INFO, MODE_PRIVATE);
        return sharedPreferences.getString("patchCode","-1");
    }

    /**
     * 在补丁安装成功后，设置当前版本补丁安装成功
     * @param patchCode 补丁版本号
     * @param isSuccess true 安装成功 false 安装失败
     * @return =-=
     */
    public static boolean putLoadPatchResult(String patchCode,boolean isSuccess){
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(PATCH_INFO, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(String.format("code%s",patchCode), isSuccess);
        return editor.commit();
    }

    /**
     * 获取对应版本补丁安装结果
     * @param patchCode 补丁版本号
     * @return true 成功 false 失败
     */
    public static boolean getLoadPatchResult(String patchCode){
        SharedPreferences sharedPreferences = TownApplicationLike.getInstance().getSharedPreferences(HomeRefreshTime, MODE_PRIVATE);
        return sharedPreferences.getBoolean(String.format("code%s",patchCode), false);
    }
}
